<template>
  <div class="project_detail_page">
    <div class="check_mod fail_mod" v-if="detailObj.status === '40'">
      <img src="@/assets/icons/fail_status.png" alt="" class="icon">
      <div class="reason_box">
        <span class="reason">驳回原因：</span>
        <span class="text">{{ detailObj.remark }}</span>
      </div>
    </div>
    <div class="check_mod success_mod" v-else-if="detailObj.status === '30'">
      <i class="el-icon-success icon"></i>
      <div class="reason_box">
        <span class="reason">融资成功</span>
      </div>
    </div>
    <div class="mod">
      <div class="title">
        <i class="icon"></i>
        项目公司
      </div>
      <div class="content">
        <el-row class="row">
          <el-col :span="8">
            <div class="cell">
              <div class="key">企业名称：</div>
              <div class="value">{{ detailObj.enterpriseName }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="cell">
              <div class="key">统一社会信用代码：</div>
              <div class="value">{{ detailObj.creditCode }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="cell">
              <div class="key">法人代表人：</div>
              <div class="value">{{ detailObj.legal }}</div>
            </div>
          </el-col>
        </el-row>
        <el-row class="row">
          <el-col :span="8">
            <div class="cell">
              <div class="key">法定代表人电话：</div>
              <div class="value">{{ detailObj.frContactPhone }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="cell">
              <div class="key">业务联系人姓名：</div>
              <div class="value">{{ detailObj.bsContactName }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="cell">
              <div class="key">业务联系人电话：</div>
              <div class="value">{{ detailObj.bsContactPhone }}</div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <div class="mod">
      <div class="title">
        <i class="icon"></i>
        项目信息
      </div>
      <div class="content">
        <el-row class="row">
          <el-col :span="8">
            <div class="cell">
              <div class="key">项目名称：</div>
              <div class="value">{{ detailObj.pname }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="cell">
              <div class="key">项目编号：</div>
              <div class="value">{{ detailObj.pcode }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="cell">
              <div class="key">项目金额(元)：</div>
              <div class="value">{{ detailObj.pamount }}</div>
            </div>
          </el-col>
        </el-row>
        <el-row class="row">
          <el-col :span="8">
            <div class="cell">
              <div class="key">项目状态：</div>
              <div class="value">{{ detailObj.pstatus }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="cell">
              <div class="key">是否有预付款：</div>
              <div class="value">{{ detailObj.psignDate }}</div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="cell">
              <div class="key">项目交付日期：</div>
              <div class="value">{{ detailObj.pdeliverDate }}</div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <div class="mod">
      <div class="title">
        <i class="icon"></i>
        最新贸易合同
      </div>
      <el-table :data="detailObj.newContractInfo" border>
        <el-table-column
          label="序号"
          type="index"
        >
        </el-table-column>
        <el-table-column
          v-for="(item, index) in contractColumns"
          :key="item.label"
          v-bind="item"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text">
              <a :href="baseURL + scope.row.url" target="_blank">预览</a>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="mod">
      <div class="title">
        <i class="icon"></i>
        最新贸易发票
      </div>
      <el-table :data="detailObj.newInvoiceInfo" border>
        <el-table-column
          v-for="item in invoiceColumns"
          :key="item.label"
          v-bind="item"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="previewBigImg(scope.row.url)">预览</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="total_box">
        共计发票金额：<span class="total">{{ detailObj.newInvoiceTotalAmount }}</span>
      </div>
    </div>
    <div class="mod">
      <div class="title">
        <i class="icon"></i>
        最新其他资料
      </div>
      <el-table :data="detailObj.newOtherInfo" border>
        <el-table-column label="序号" type="index" width="200px"></el-table-column>
        <el-table-column
          v-for="item in otherColumns"
          :key="item.label"
          :label="item.label"
          :proop="item.prop"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text">
              <a :href="baseURL + scope.row.url" target="_blank">查看</a>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="mod">
      <div class="title">
        <i class="icon"></i>
        历史合作信息
      </div>
      <div class="content">
        <el-row class="row">
          <el-col :span="8">
            <div class="cell">
              <div class="key">是否有历史合作：</div>
              <div class="value">{{ detailObj.cooperateType === '0' ? '是' : '否' }}</div>
            </div>
          </el-col>
          <el-col :span="8" v-if="detailObj.cooperateType === '0'">
            <div class="cell">
              <div class="key">最早合作签订日期：</div>
              <div class="value">{{ detailObj.earliestSignDate }}</div>
            </div>
          </el-col>
          <el-col :span="8" v-if="detailObj.cooperateType === '0'">
            <div class="cell">
              <div class="key">最后合作签订日期：</div>
              <div class="value">{{ detailObj.latestSignDate }}</div>
            </div>
          </el-col>
        </el-row>
        <el-row class="row" v-if="detailObj.cooperateType === '0'">
          <el-col :span="8">
            <div class="cell">
              <div class="key">共计合作年限：</div>
              <div class="value">{{ detailObj.cooperationPeriod }}</div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <div v-if="detailObj.cooperateType === '0'">
      <div class="mod">
        <div class="title">
          <i class="icon"></i>
          贸易合同
        </div>
        <el-table :data="detailObj.contractInfo" border>
          <el-table-column
            label="序号"
            type="index"
          >
          </el-table-column>
          <el-table-column
            v-for="(item, index) in contractColumns"
            :key="item.label"
            v-bind="item"
          >
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text">
                <a :href="baseURL + scope.row.url" target="_blank">预览</a>
              </el-button>
              <el-button type="text" @click="delContractRow(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="mod">
        <div class="title">
          <i class="icon"></i>
          贸易发票
        </div>
        <el-table :data="detailObj.invoiceInfo" border>
          <el-table-column
            v-for="item in invoiceColumns"
            :key="item.label"
            v-bind="item"
          >
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="previewBigImg(scope.row.url)">预览</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="total_box">
          共计发票金额：<span class="total">{{ detailObj.invoiceTotalAmount }}</span>
        </div>
      </div>
      <div class="mod">
        <div class="title">
          <i class="icon"></i>
          其他资料
        </div>
        <el-table :data="detailObj.otherInfo" border>
          <el-table-column label="序号" type="index" width="200px"></el-table-column>
          <el-table-column
            v-for="item in otherColumns"
            :key="item.label"
            :label="item.label"
            :proop="item.prop"
          >
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text">
                <a :href="baseURL + scope.row.url" target="_blank">查看</a>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div v-if="detailObj.status === '10'">
      <div class="mod">
        <div class="title">
          <i class="icon"></i>
          申请审核
        </div>
        <div class="check_content">
          <div class="check_line">
            <div class="key">审核状态</div>
            <div class="value">
              <el-button
                class="btn left_btn"
                :type="status === 30 ? 'primary' : ''"
                @click="status = 30"
              >审核通过</el-button>
              <el-button
                class="btn right_btn"
                :type="status === 40 ? 'danger' : ''"
                @click="status = 40"
              >驳回修改</el-button>
            </div>
          </div>
          <div class="check_line">
            <div class="key key_reason">审核原因</div>
            <div class="value">
              <el-form
                ref="checkForm"
                :model="checkForm"
                :rules="status === 40 ? checkRules : {}"
              >
                <el-form-item prop="remark" >
                  <el-input
                    type="textarea"
                    placeholder="请输入审核建议内容"
                    :rows="5"
                    class="textarea"
                    v-model="checkForm.remark"
                  ></el-input>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </div>
      <div class="mod">
        <!-- <div class="title">
          <i class="icon"></i>
          签署协议
        </div> -->
        <div class="content">
          <div class="btns">
            <el-button
              class="btn"
              plain
            >取 消</el-button>
            <el-button
              class="btn btn_right"
              :type="status === 30 ? 'primary' : status === 40 ? 'danger' : ''"
              :disabled="status !== 30 && status !== 40"
              :loading="checkLoading"
              @click="check"
            >
              {{ status === 40 ? '驳回资产' : '审核通过'}}
            </el-button>
          </div>
        </div>
      </div>
    </div>
    <CustomDialog
      title="查看内容"
      width="70%"
      :visible="dialogVisible"
      @close="dialogVisible = false"
    >
      <div class="mask_dialog_content">
        <div class="img_box">
          <img :src="baseURL+ previewBigImgUrl" alt="" class="img" />
        </div>
      </div>
    </CustomDialog>
  </div>
</template>

<script>
import { projectDetail, projectCheck } from '@/api/production'
import CustomDialog from '@/customComponents/dialog'

export default {
  components: {
    CustomDialog,
  },
  data() {
    return {
      baseURL: process.env.NODE_ENV === 'development' ? process.env.VUE_APP_BASE_API :
        process.env.VUE_APP_HTTP_REQUEST_ADDRESS,
      id: '',
      detailObj: {
        enterpriseName: '', // 企业名称
        creditCode: '', // 统一社会信用代码
        legal: '', // 法定代表人
        frContactPhone: '', // 法定代表人电话
        bsContactName: '', // 业务联系人姓名
        bsContactPhone: '', // 业务联系人电话
        pname: '', // 项目名称
        pcode: '', // 项目编号
        pamount: '', // 项目金额（元）
        pstatus: '', // 项目状态，字典（p_status）
        psignDate: '', // 项目签署日期
        pdeliverDate: '', // 项目交付日期


        newContractInfo: [], // 最新贸易合同数据
        newInvoiceInfo: [], // 最新贸易发票数据
        newInvoiceTotalAmount: 0, // 最新发票总金额
        newOtherInfo: [], // 最新其他资料

        cooperateType: '', // 是否有历史合作 0:是 1:否
        earliestSignDate: '', // 最早合作签订日期
        latestSignDate: '', // 最后合作签订日期
        cooperationPeriod: '', // 共计合作年限

        contractInfo: [], // 贸易合同数据
        invoiceInfo: [], // 贸易发票数据
        invoiceTotalAmount: 0, // 发票总金额
        otherInfo: [], // 其他资料

      },
      contractColumns: [], // 贸易合同列
      invoiceColumns: [], // 贸易发票列
      otherColumns: [], // 其他资料列
      dialogVisible: false, // 预览图弹窗
      previewBigImgUrl: '', // 预览图
      enterpriseBankObj: {
        accountName: '',
        bankNumber: '',
        bankBranch: '',
        bankCode: '',
      }, // 银行信息
      status: '', // 提交审核用，传30成功，传40失败
      checkForm: {
        remark: '', // 审核原因
      },
      checkRules: {
        remark: [
          { required: true, trigger: ['change', 'blur'], message: '请输入审核建议内容' },
        ]
      },
      checkLoading: false,
    }
  },
  created() {
    this.contractColumns = [
      {
        label: '合同名称',
        prop: 'contractName'
      },
      {
        label: '合同编号',
        prop: 'contractCode'
      },
      {
        label: '合同金额',
        prop: 'contractAmount'
      },
      {
        label: '合同签订日期',
        prop: 'contractDateStart',
        type: 'date',
      },
      {
        label: '合同结束日期',
        prop: 'contractDateEnd',
        type: 'date',
      },
    ]
    this.invoiceColumns = [
      {
        label: '序号',
        type: 'index',
      },
      {
        label: '发票类型',
        prop: 'invoiceTypeDesc'
      },
      {
        label: '发票代码',
        prop: 'code'
      },
      {
        label: '发票号码',
        prop: 'no'
      },
      // {
      //   label: '发票校验码',
      //   prop: ''
      // },
      {
        label: '发票金额(元)',
        prop: 'sumAmount'
      },
      {
        label: '开票日期',
        prop: 'date'
      },
      {
        label: '购买方',
        prop: 'buyerName'
      },
      {
        label: '销售方',
        prop: 'sellerName'
      },
    ]
    this.otherColumns = [
      {
        label: '文件名称',
        type: 'fileName'
      },
    ]
  },
  watch: {
    status: {
      handler: function (newVal, oldVal) {
        if (newVal !== 40) {
          this.$refs.checkForm.clearValidate()
        }
      }
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id
      this.getProjectDetail(this.id)
    }
  },
  methods: {
    getProjectDetail() {
      projectDetail(this.id).then((res) => {
        this.detailObj = res.data
        Object.keys(this.detailObj).forEach((key) => {
          if (typeof(this.detailObj[key]) !== 'object') {
            this.detailObj[key] = res.data[key]
          }
        })
        this.detailObj.newContractInfo = JSON.parse(res.data?.newContractInfo || '[]')
        this.detailObj.newInvoiceInfo = JSON.parse(res.data?.newInvoiceInfo || '[]')
        this.detailObj.newOtherInfo = JSON.parse(res.data?.newOtherInfo || '[]')
        this.detailObj.contractInfo = JSON.parse(res.data?.contractInfo || '[]')
        this.detailObj.invoiceInfo = JSON.parse(res.data?.invoiceInfo || '[]')
        this.detailObj.otherInfo = JSON.parse(res.data?.otherInfo || '[]')

        console.log(this.detailObj)
      })
    },
    // 预览发票大图
    previewBigImg(url) {
      this.previewBigImgUrl = url
      this.dialogVisible = true
    },
    // 项目审核
    check() {
      // status=30&remark=
      if (this.status === 40) {
        this.$refs.checkForm.validate((valid) => {
          if (valid) {
            this.projectCheck()
          }
        })
      } else {
        this.projectCheck()
      }
    },
    projectCheck() {
      this.checkLoading = true
      projectCheck(
        this.id,
        {
          status: this.status,
          remark: this.checkForm.remark,
          userType: this.userType,
        }
      ).then((res) => {
        this.checkLoading = false
        this.$message({
          type: 'success',
          message: this.status === 30 ? '审核成功' : '驳回成功',
          onClose: () => {
            this.$router.back()
          }
        })
      }).catch((error) => {
        this.checkLoading = false
        this.$router.back()
      })
    },
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/styles/variables.scss';

.project_detail_page {
  .check_mod {
    border-radius: 12px;
    padding: 26px 30px;
    margin-bottom: 16px;
    display: flex;
    align-items: center;
  }
  .fail_mod {
    border: 1px solid #FF4D4F;
    background-color: #FFF7F6;
    .icon {
      display: block;
      width: 48px;
      height: 48px;
      margin-right: 10px;
    }
    .reason_box {
      color: #FF4D4F;
      font-weight: 600;
      .reason {
        font-size: 20px;
        margin-right: 8px;
      }
      .text {
        font-size: 14px;
      }
    }
  }
  .success_mod {
    border: 1px solid rgb(194, 231, 176);
    background-color: rgb(240, 249, 235);
    .icon {
      font-size: 48px;
      margin-right: 10px;
      color: rgb(103, 194, 58);
    }
    .reason_box {
      color: rgb(103, 194, 58);
      font-weight: 600;
      .reason {
        font-size: 20px;
        margin-right: 8px;
      }
    }
  }
  .mod {
    background-color: #fff;
    box-shadow: 0 1px 2px 0 rgba(1, 24, 70, 0.09);
    border-radius: 6px;
    margin-bottom: 16px;
    padding: 20px 16px 24px;
    .title {
      color: #333;
      font-size: 16px;
      font-weight: 600;
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      .icon {
        width: 3px;
        height: 16px;
        border-radius: 1px;
        background-color: $theme;
        margin-right: 5px;
      }
    }
    .content {
      .row {
        margin-bottom: 38px;
        .cell {
          display: flex;
          align-items: center;
          .key {
            color: #777;
            font-size: 14px;
            width: 195px;
            text-align: right;
          }
          .value {
            color: #333;
            font-size: 14px;
          }
        }
      }
      .btns {
        display: flex;
        justify-content: center;
        align-items: center;
        .btn {
          width: 140px;
        }
        .btn_right {
          margin-left: 24px;
        }
      }
    }
    .check_content {
      padding: 40px 300px 40px 146px;
      .check_line {
        display: flex;
        align-items: center;
        margin-bottom: 20px;
        &:last-child {
          margin-bottom: 0;
        }
        .key {
          color: #777;
          font-size: 14px;
          margin-right: 10px;
          &.key_reason {
            align-self: flex-start;
            margin-top: 20px;
          }
        }
        .value {
          flex: 1 1 auto;
          .btn {
            width: 120px;
          }
          .right_btn {
            margin-left: 20px;
          }
          .textarea {
            width: 100%;
          }
        }
      }
    }
    .total_box {
      height: 32px;
      line-height: 32px;
      background-color: #FFFAF4;
      color: #333;
      font-size: 14px;
      font-weight: 500;
      border-radius: 2px;
      padding-left: 28px;
      margin-top: 20px;
      .total {
        color: #FF4D4F;
        font-size: 20px;
      }
    }
  }
}
</style>
